package cn.iocoder.yudao.module.signature.controller.app.application;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.signature.controller.admin.application.vo.ApplicationInfoPageReqVO;
import cn.iocoder.yudao.module.signature.controller.admin.application.vo.ApplicationInfoRespVO;
import cn.iocoder.yudao.module.signature.controller.admin.application.vo.ApplicationInfoSaveReqVO;
import cn.iocoder.yudao.module.signature.dal.dataobject.application.ApplicationInfoDO;
import cn.iocoder.yudao.module.signature.enums.EnableType;
import cn.iocoder.yudao.module.signature.service.application.ApplicationInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "app - 应用")
@RestController
@RequestMapping("/signature/application-info")
@Validated
public class AppApplicationInfoController {

    @Resource
    private ApplicationInfoService applicationInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建应用")
    @PreAuthenticated
    public CommonResult<Long> createApplicationInfo(@Valid @RequestBody ApplicationInfoSaveReqVO createReqVO) {

        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        createReqVO.setUserId(loginUser.getId());
        return success(applicationInfoService.createApplicationInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新应用")
    @PreAuthenticated
    public CommonResult<Boolean> updateApplicationInfo(@Valid @RequestBody ApplicationInfoSaveReqVO updateReqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        updateReqVO.setUserId(loginUser.getId());
        applicationInfoService.updateApplicationInfo(updateReqVO);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得应用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<ApplicationInfoRespVO> getApplicationInfo(@RequestParam("id") Long id) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        ApplicationInfoDO applicationInfo = applicationInfoService.getApplicationInfo(id,loginUser.getId());
        return success(BeanUtils.toBean(applicationInfo, ApplicationInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得应用分页")
    @PreAuthenticated
    public CommonResult<PageResult<ApplicationInfoRespVO>> getApplicationInfoPage(@Valid ApplicationInfoPageReqVO pageReqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        pageReqVO.setUserId(loginUser.getId());
        pageReqVO.setApplicationStatus(String.valueOf(EnableType.ENABLE.getType()));
        PageResult<ApplicationInfoDO> pageResult = applicationInfoService.getApplicationInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApplicationInfoRespVO.class));
    }

    @GetMapping("/getInstallAppList")
    @Operation(summary = "获取可安装应用列表")
    public CommonResult<List<ApplicationInfoRespVO>> getInstallAppList( ) {
        List<ApplicationInfoRespVO> applicationInfoList = applicationInfoService.getInstallAppList();
        return success(applicationInfoList);
    }


    @PostMapping("/ipa-info-extractor")
    @Operation(summary = "获取应用 IPA 包信息")
    @PreAuthenticated
    public CommonResult<?> getIpaInfoExtractor(@RequestParam("file") MultipartFile file) {
        return success(applicationInfoService.getIpaInfoExtractor(file));
    }

    @PostMapping("/delete")
    @Operation(summary = "删除数据（单个或者批量）")
    @PreAuthenticated
    public CommonResult<Boolean> delete(@RequestBody List<Long> ids) throws Exception{
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser, "用户未登录");
        applicationInfoService.delete(ids, loginUser.getId());
        return success(Boolean.TRUE);
    }

}